package cn.iocoder.yudao.module.erp.controller.app.erpSupplier;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.app.auth.vo.AppErpSupplierSaveReqVO;
import cn.iocoder.yudao.module.erp.controller.app.erpSupplier.vo.AppErpSupplierUpdatePasswordReqVO;
import cn.iocoder.yudao.module.erp.controller.app.erpSupplier.vo.AppErpSupplierUpdateReqVO;
import cn.iocoder.yudao.module.erp.controller.app.erpSupplier.vo.ErpSupplierInfoRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierDO;
import cn.iocoder.yudao.module.erp.service.purchase.ErpSupplierService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "WEB - ERP供应商信息接口")
@RestController
@RequestMapping("/erp/supplier/user")
@Validated
@Slf4j
public class WebErpSupplierController {

    @Resource
    private ErpSupplierService erpSupplierService;


    @PostMapping("/register")
    @Operation(summary = "供应商注册")
    @PermitAll
    public CommonResult<Long> createSupplier(@Valid @RequestBody AppErpSupplierSaveReqVO createReqVO) {
        return success(erpSupplierService.createSupplier(createReqVO));
    }


    @GetMapping("/get")
    @Operation(summary = "获得供应商基本信息")
    public CommonResult<ErpSupplierInfoRespVO> getUserInfo() {
        ErpSupplierDO supplier = erpSupplierService.getSupplier(getLoginUserId());
        ErpSupplierInfoRespVO bean = BeanUtils.toBean(supplier, ErpSupplierInfoRespVO.class);
        return success(bean);
    }

    @PostMapping("/update")
    @Operation(summary = "修改基本信息")
    public CommonResult<Boolean> updateUser(@RequestBody @Valid AppErpSupplierUpdateReqVO reqVO) {
        erpSupplierService.updateSupplier(reqVO);
        return success(true);
    }

    @GetMapping("/commit")
    @Operation(summary = "提交资质认证")
    public CommonResult<Boolean> commit() {
        erpSupplierService.commit();
        return success(true);
    }


    @PostMapping("/updatePassword")
    @Operation(summary = "修改用户密码", description = "用户修改密码时使用")
    public CommonResult<Boolean> updateUserPassword(@RequestBody @Valid AppErpSupplierUpdatePasswordReqVO reqVO) {
        erpSupplierService.updateSupplierPassword(reqVO);
        return success(true);
    }

}
